When using the nkGraphics component within the Nilkins Engine, the first step is to initialize it through the API. This is required to prepare the component, allocate all basic resources it needs, and launch the back end requested.
Basic initialization go through including :
And then using the class through :
The System is a singleton that can be accessed through the getInstance method. Calling the initialize method on it will trigger all above-mentioned preparation steps. Checking on the result can let you know if the component could initialize itself correctly or not. If it is not the case, the component cannot be used.
Once the engine is initialized, what is needed next, is to create a RenderContext. This represents a visible "surface" in which the component will render to. What contexts can do will be covered in a later tutorial. For now, we will just create one, requesting it to be linked to a window. First, include :
Which allows to do :
In a nutshell, what we now have is a rendering context, that has been linked to a window that should pop automatically on screen. Note that under the hood, nkGraphics uses nkWinUi for the window handling. Back to the program, if you launch current code the window will close itself right away, and program will exit.
What we miss is the rendering loop. As such, the program automatically finishes and clean everything. Again, more details about the rendering loop will come in time in a dedicated tutorial, as this can be a complex challenge. For now, what we will do is simply ask the component to run by itself :
The run method takes a RenderContext as a parameter. This is required to know which context the rendering loop should render to.
With this code added, running the program will result in something like :
There is not a lot to do with this window apart from closing it, for now. However, we also have a last step to think about when exiting the program : requesting a clean of the component's memory. For that, after the run loop call, add :
This method is from the MaybeSingletonClass, and will delete the instance. The System automatically calls its shutdown method during deletion, which means all memory will be cleaned for a safe exiting of the program.
And with all of that, congratulations ! You now have the nkGraphics component set up and ready to be used.
Currently the engine is quite silent about what is happening inside. This is because we did not plug any logger inside it. Let's change that so that we can witness what the component has to tell us. We will use the provided console logger for easiness. If your program doesn't use the console, you can use the file logger, rather. First, include :
And, before calling the initialization method, add this :
Launching the program, you should notice a difference in the console :
Whenever a problem occurs, the component will use the logger provided to give a shout. Plugging one that is comfortable to use is important to be able to troubleshoot problems easily.
The component being ready to be used, and now giving all the information we need, it is time to move on to next tutorials, from which more and more capabilities of the engine will be demonstrated !